/**
 * 一级分类添加修改
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
        name: function(value){
            if(value.length > 20){ return "分类名称长度超过20了，请调整后再试"; };
        },
        sort: function(value){
            if (!/^[1-9]\d*$/.test(value)) {
                return "排序值只能是正整数";
            }
            if (value.length > 8){
                return "排序字段长度不能超过8位";
            }
        },tariffName:function (value) {
            if(!/^[A-Za-z0-9\u4e00-\u9fa5]+$/.test(value)){
                return "计价器名称只能输入字母、数字、汉字";
            }
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var openId = layer.open({type: 3});
        var data = obj.field;
        data.showType = 2;
        var url = "/showCateg/addEdit.action";
        if(!obj.field.tariffStatus){
            layer.msg("计价器状态必选");
            return false;
        }
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                layer.msg(result.message);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframeMsg(parent, result.message, "zmn-table");
                        parent.location.reload();
                    });
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })

        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });


    //对外暴露的接口
    exports('art_categ_show_detail', {});
});